package com.taoyuan.hotel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taoyuan.hotel.dao.DepartmentDao;
import com.taoyuan.hotel.entity.Department;
import com.taoyuan.hotel.service.intf.DepartmentService;
import com.taoyuan.hotel.vo.PageBean;
import com.taoyuan.hotel.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;

    @Override
    public R save(Department department) {
        //执行新增操作
        if (dao.add(department) > 0) {
            //成功
            return R.ok();
        } else {
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Department> pageInfo = new PageInfo<>(dao.all());
        //3.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(int id) {
        if (id > 0) {
            if (dao.del(id) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    @Override
    public R update(Department department) {
        if (dao.update(department) > 0) {
            //修改成功
            return R.ok();
        } else {
            //修改失败
            return R.fail();
        }
    }

    @Override
    public PageBean queryPageWhere(String deptName, int page, int limit) {
        //1.验证 查询条件 如果为"" 那么赋值为null
        if (!StringUtils.hasLength(deptName)){
            deptName=null;
        }else {
            deptName="%"+deptName+"%";//包含模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Department> pageInfo=new PageInfo<>(dao.selectWhere(deptName));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if (ids!=null && ids.length>0){
            if (dao.delBatch(ids)>0){
                return R.ok();
            }else {
                return R.fail();
            }
        }else {
            return R.fail();
        }
    }
}
